using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.MPE;

public class GT_NodeEditorWindow : EditorWindow
{
    #region Variables
    public static GT_NodeEditorWindow curWindow;
    public GT_NodePropertyView propertyView;
    public GT_NodeWorkView workView;

    public GT_NodeGraph curGraph=null;

    public float viewPrecentage = 0.75f;
    #endregion

    #region Main Methods
    public static void InitEditorWindow()
    {
        curWindow = (GT_NodeEditorWindow)EditorWindow.GetWindow<GT_NodeEditorWindow>();
        curWindow.title = "Node Editor";

        CreateViews();
    }

    private void OnEnable()
    {
        //Debug.Log("Enabled Window");
    }

    private void OnDestroy()
    {
        //Debug.Log("OnDestroy Window");
    }

    private void Update()
    {
        //Debug.Log("Update Window");
    }

    private void OnGUI()
    {
        //Check for null views
        if (propertyView == null || workView == null)
        {
            CreateViews();
            return;
        }

        //Get and process the current Event
        Event e = Event.current;
        ProcessEvent(e);

        //Update Views
        workView.UpdateView(position, new Rect(0f, 0f, viewPrecentage, 1f),e, curGraph);
        propertyView.UpdateView(new Rect(position.width, position.y, position.width, position.height),
                                new Rect(viewPrecentage, 0f, 1 - viewPrecentage, 1f),e, curGraph);

        Repaint();
    }
    #endregion

    #region Utility Methods
    private static void CreateViews()
    {
        if (curWindow != null)
        {
            curWindow.propertyView = new GT_NodePropertyView();
            curWindow.workView = new GT_NodeWorkView();
        }
        else
        {
            curWindow = (GT_NodeEditorWindow)EditorWindow.GetWindow<GT_NodeEditorWindow>();
        }
    }

    private void ProcessEvent(Event e)
    {
        if (e.type == EventType.KeyDown && e.keyCode == KeyCode.LeftArrow)
        {
            viewPrecentage -= 0.01f;
        }
        if (e.type == EventType.KeyDown && e.keyCode == KeyCode.RightArrow)
        {
            viewPrecentage += 0.01f;
        }
    }
    #endregion




}
